#pragma once

#include "stdafx.h"
#include "vec3.h"
#include "Camera.h"
#include "Projectile.h"
#include "Node.h"
#include "LoadTGA.h"
#include "printw.h"
#include "GBox.h"
#include "Frustum.h"
#include "Player.h"
#include "Room.h"
#include "LuaScript.h"
#include "Ball.h"
#include "Random.h"
#include "Timer.h"
#include "Scene.h"
#include "Partitioning.h"
#include "VBO.h"

//Mouse Info
typedef struct {
	bool mLButtonUp;
	bool mRButtonUp;
	bool middleButtonUp;
	int  lastX, lastY;
} theMouseInfo;

class myApplication : public Partition
{
public:
	// this is a constructor, it is implemented in myapplication.cpp
	myApplication();
	// this is a destructor, it is implemented in myapplication.cpp
	~myApplication();

//	bool Start( int argc, char **argv );

	bool Init();

	void renderScene(void);
	void Update(float delta);

	void changeSize(int w, int h);

	void inputKey(int key, int x, int y);
	void KeyboardDown(unsigned char key, int x, int y);
	void KeyboardUp(unsigned char key, int x, int y);
	void MouseMove (int x, int y);
	void MouseClick(int button, int state, int x, int y);

	static myApplication* getInstance();
	void drop();

	// Calculate the FPS, returns 
	void calculateFPS();
	// realtime loop control
	long timelastcall;
	// realtime loop control - frequency
	float frequency;

private:
	
	static myApplication *s_pInstance;
	// Texture mapping
//	GLuint	texture[16];			// Storage For Grass Texture
	TextureImage grassTexture[5];
	TextureImage skyTexture[6];

	// Camera
	vec3 theCameraPosition;
	vec3 theCameraDirection;
	float angle;

	void Draw3DEnvironment();

	void moveMeForward(bool mode, float timeDiff);
	void moveMeSideway(bool mode, float timeDiff);

	void renderGround(void);
	void renderSky(void);
	void renderHUD(void);

	//  The number of frames
	int frameCount;
	//  Number of frames per second
	float fps;
	//  currentTime - previousTime is the time elapsed
	//  between every call of the Idle function
	int currentTime, previousTime;
	// Display FPS
	void drawFPS();
	void drawCrosshair();

	bool myKeys[255];
	theMouseInfo mouseInfo;

	// The camera
	Camera* theCamera;

	// Accumuation Buffer
	float aMult, aAccum, aReturn;

	// The projectiles
	CProjectile* theProjectiles;
	const static int MAXPROJECTILES = 50;
	int m_iProjectileIndex;
	int projectilespeed;

	// The sound engine
	ISoundEngine* theSoundEngine;
	ISound* Sound_Footstep;

	float calculateDistanceMoved( float acceleration, float maxVelocity, float timeDiff );
	float previousVelocity;

	// Draw the healthbar in the OpenGL window
	int m_iHeightOfBar;
	int m_iWidthOfBar;
	void DrawHealthBar(const int m_iHealth, const int m_iMaxHealth);

//	Node gBoxes;
//	GBox gBox[gBox_GridSize][gBox_GridSize][gBox_GridSize];

	Frustum frustum;

	void FrustumCheckNodes(Node *box);
	bool bMoveCamera;
	
	void PlayerCollide(void);

	Room *room;

	float attenuation[3];

	LuaScript* lua;

//	BallManager* balls;
	int BallsInFrustum;

	bool bPauseGame;
	bool bWinGame, bLoseGame;

public:
	// Update timers
	int ChildrenUpdateTimer,
		FrustumUpdateTimer,
		BallsCollideTimer,
		PlayerCollideTimer;
	double microTimer; // timer for microseconds
};